package org.example.demo8;

import lombok.SneakyThrows;

import java.lang.reflect.Field;

/**
 * 获取接口的常量
 * @author Gong.Yang
 */
public class Main {
    @SneakyThrows
    public static void main(String[] args) {
        NF2 bean = new NF2();
        Class<?> iService = getIService(bean);
        Class<?> contract = getContract(iService);
        Field name = contract.getDeclaredField("name");
        System.out.println(name.get(null));
    }

    public static Class<?> getIService(Object bean) {
        String beanClassSimpleName = bean.getClass().getSimpleName();
        Class<?>[] interfaces = bean.getClass().getInterfaces();
        for (Class<?> anInterface : interfaces) {
            String interfaceSimpleName = anInterface.getSimpleName();
            if (!("I"+beanClassSimpleName).equals(interfaceSimpleName)){
                continue;
            }
            return anInterface;
        }
        throw new RuntimeException();
    }


    public static Class<?> getContract(Class<?> iService) {
        for (Class<?> declaredClass : iService.getDeclaredClasses()) {
            if (declaredClass.getSimpleName().equals("Contract")) {
                return declaredClass;
            }
        }
        throw new RuntimeException();
    }
}
